<?php
/**
 * Vehicle plugin class
 *
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @version	1.0
 * @link	http://developer.resmania.com/api
 * @since  	08-2009
 */
class RM_Plugin_Vehicle extends RM_Plugin {
    public function  __construct()
    {
        $this->name = 'Vehicle';
    }

    public function disable()
    {
        throw new RM_Exception(RM_Environment::getInstance()->getTranslation()->_('Admin.Vehicle.Main', 'DisableAlert'));    
    }

    /**
     * This method will return node object for main admin menu tree.
     * Every child classes could overload this method to return any of the node object.
     * If there is no need to present a plugin in the main admin tree overloaded method should return NULL
     *
     * @return stdClass | null
     */
    public function getNode()
    {
        return null;
    }

    /**
     * This method will return node object for configuration admin menu tree.
     * Every child classes could overload this method to return any of the node object.
     * If there is no need to present a plugin in the configuration admin tree overloaded method should return NULL
     *
     * @return stdClass | null
     */
    public function getConfigNode()
    {
        return null;
    }

    public function upgrade(){
        $pluginViewFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'plugins',
            $this->name,
            'views'
        ));
        $pluginFolderPath = implode(DIRECTORY_SEPARATOR, array(
            $pluginViewFolderPath,
            'user',
            'scripts',
            'Unit',
            'details',
            'vehicle_unitdetails.phtml'
        ));
        $mainFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details',
            'vehicle_unitdetails.phtml'
        ));
        $renameStatus = rename($pluginFolderPath, $mainFolderPath);
        RM_Filesystem::deleteFolder($pluginViewFolderPath);
    }

    public function install()
    {
        parent::install();

        $model = new RM_UnitTypeConfig();
        $model->register(3);

        $pluginViewFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'plugins',
            $this->name,
            'views'
        ));
        $pluginFolderPath = implode(DIRECTORY_SEPARATOR, array(
            $pluginViewFolderPath,
            'user',
            'scripts',
            'Unit',
            'details',
            'vehicle_unitdetails.phtml'
        ));
        $mainFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details',
            'vehicle_unitdetails.phtml'
        ));
        $renameStatus = rename($pluginFolderPath, $mainFolderPath);
        RM_Filesystem::deleteFolder($pluginViewFolderPath);

        return $renameStatus;
    }

    public function uninstall()
    {
        parent::uninstall();

        $model = new RM_UnitTypes();
        $type = $model->findByName('Vehicle');

        $unitModel = new RM_Units();
        $unitModel->update(
            array(
                'type_id' => RM_UnitTypes::DEFAULT_TYPE,
                'published' => 0
            ),
            'type_id='.$type->id
        );

        $file = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details',
            'vehicle_unitdetails.phtml'
        ));
        RM_Filesystem::deleteFile($file);
    }
}